define([
	'jquery',
	'backbone',
	'hbs!templates/books',
], function ($, Backbone, template) {

	BooksView = Backbone.View.extend({

		initialize: function () {
			if (this.model) {
				this.model.on('reset', this.render, this);
			}
		},
		
		events: {
			"click .sort": "processSort"
		},
		
		processSort: function(event) {
			var sortAttr = $(event.target).attr('id');
			var sortConfig = this.model.sortConfig;
			var sortAttrIndex = 0;
			for (var i = 0; i < sortConfig.attrs.length;i++) {
				var attr = sortConfig.attrs[i];
				if (sortAttr == attr.attr) {
					attr.asc = !attr.asc;
					attr.active = true;
					sortAttrIndex = i;
				} else {
					attr.active = false;
				}
			}
			this.model.sortAttrIndex = sortAttrIndex;
			
			this.render();
		},
		
		
		addOne:function(book) {
			var tiles = this.model.viewType == "tiles" ? true: false;
			book.set('tiles', tiles);
			new BookView({
				model: book,
				el: this.$el.find("#" + book.get('id'))
			});
			book.trigger('change');
		},

		render: function () {
			var me = this;
			// Apply sort
			
			
			this.model.sort();
			
			var models = this.model.models;
			
			// Apply filter
			var searchKey = this.model.searchKey;
			if (searchKey) {
				models = this.model.search(searchKey);
			}
			
			// Apply paging
			var pages = new Array();
			var pageId = this.model.pageId - 1;
			var viewType = this.model.viewType;
			var prevLink = "";
			var nextLink = "";
			if (pageId != null) {
				var maxItemOfPage = this.model.maxItemOfPage;
				var length = models.length;
				var start = pageId * maxItemOfPage;
				var end = length;
				if (start >= length) {
					start = 0;
				} else {
					if (start + maxItemOfPage < length) {
						end = start + maxItemOfPage;
					};
				}
				models = models.slice(start, end);
				var numberOfPage = Math.floor(length/maxItemOfPage);
				if (length % maxItemOfPage > 0) {
					numberOfPage = numberOfPage + 1;
				}
				for (var i = 0; i < numberOfPage; i++) {
					var page = {};
					page.id = i + 1;
					page.link="#books/" + viewType + "/" + page.id;
					if (i == pageId) {
						page.active = true;
					}
					pages.push(page);
				}
				var prevIndex =  pageId;
				if (prevIndex == 0) {
					prevIndex = 1;
				}
				var nextIndex =  pageId + 2;
				if (nextIndex > numberOfPage) {
					nextIndex = numberOfPage;
				}
				prevLink = "#books/" + viewType + "/" + prevIndex;
				nextLink = "#books/" + viewType + "/" + nextIndex;
			}
			
			
			//
			var row = 4;
			if (viewType == "tiles") {
				for (var i = 0; i < models.length; i++) {
					if ((i + 1) % row == 0 && i != 0) {
						models[i].br = true;
					} else {
						models[i].br = false;
					}
				}
				models[models.length - 1].br = true;
			}
			//
			
			var json = {viewType: viewType, models: models, pages : pages, sortConfig: this.model.sortConfig, 
					searchKey: this.model.searchKey,
					prevLink: prevLink, nextLink: nextLink,
					tiles: viewType == "tiles" ? true: false};
			var html = template(json);
			this.$el.html(html);
			var sortEs = this.$el.find('.sort');
			$(sortEs).bind('click', function(event){
				me.processSort(event);
			});
			var input = $.find('.book-search');
			$(input).keyup(function(event){
				if (event.keyCode == 13) {
					var text = $(this).val();
					me.model.searchKey = text;
					me.render();
				}
			});
			// Render for every book
			if (this.model) {
				for(var i = 0; i < models.length; i++) {
					var model = models[i];
					model.set('viewType', this.model.get('viewType'));
					this.addOne(model);
				}
			}
		}
	});

	return BooksView;
});
